<?php
// +----------------------------------------------------------------------
// | ThinkPHP
// +----------------------------------------------------------------------
// | Copyright (c) 2007 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
// $Id: common.php 2601 2012-01-15 04:59:14Z liu21st $

//公共函数
function toDate($time, $format = 'Y-m-d H:i:s') {
	if (empty ( $time )) {
		return '';
	}
	$format = str_replace ( '#', ':', $format );
	return date ($format, $time );
}

function getStatus($status, $imageShow = true) {
	switch ($status) {
		case 0 :
			$showText = '禁用';
			$showImg = '<IMG SRC="__PUBLIC__/Images/locked.gif" WIDTH="20" HEIGHT="20" BORDER="0" ALT="禁用">';
			break;
		case 2 :
			$showText = '待审';
			$showImg = '<IMG SRC="__PUBLIC__/Images/prected.gif" WIDTH="20" HEIGHT="20" BORDER="0" ALT="待审">';
			break;
		case - 1 :
			$showText = '删除';
			$showImg = '<IMG SRC="__PUBLIC__/Images/del.gif" WIDTH="20" HEIGHT="20" BORDER="0" ALT="删除">';
			break;
		case 1 :
		default :
			$showText = '正常';
			$showImg = '<IMG SRC="__PUBLIC__/Images/ok.gif" WIDTH="20" HEIGHT="20" BORDER="0" ALT="正常">';

	}
	return ($imageShow === true) ?  $showImg  : $showText;

}

function getNodeGroupName($id) {
	if (empty ( $id )) {
		return '未分组';
	}
	if (isset ( $_SESSION ['nodeGroupList'] )) {
		return $_SESSION ['nodeGroupList'] [$id];
	}
	$Group = D ( "Group" );
	$list = $Group->getField ( 'id,title' );
	$_SESSION ['nodeGroupList'] = $list;
	$name = $list [$id];
	return $name;
}

function showStatus($status, $id) {
	switch ($status) {
		case 0 :
			$info = '<a href="javascript:resume(' . $id . ')">恢复</a>';
			break;
		case 2 :
			$info = '<a href="javascript:pass(' . $id . ')">批准</a>';
			break;
		case 1 :
			$info = '<a href="javascript:forbid(' . $id . ')">禁用</a>';
			break;
		case - 1 :
			$info = '<a href="javascript:recycle(' . $id . ')">还原</a>';
			break;
	}
	return $info;
}


function getGroupName($id) {
	if ($id == 0) {
		return '无上级组';
	}
	if ($list = F ( 'groupName' )) {
		return $list [$id];
	}
	$dao = D ( "Role" );
	$list = $dao->select( array ('field' => 'id,name' ) );
	foreach ( $list as $vo ) {
		$nameList [$vo ['id']] = $vo ['name'];
	}
	$name = $nameList [$id];
	F ( 'groupName', $nameList );
	return $name;
}

function pwdHash($password, $type = 'md5') {
	return hash ( $type, $password );
}

/**
 * 将返回的数据集转换成树
 * @param  array   $list  数据集
 * @param  string  $pk    主键
 * @param  string  $pid   父节点名称
 * @param  string  $child 子节点名称
 * @param  integer $root  根节点ID
 * @return array          转换后的树
 */
function list_to_tree($list, $pk = 'id', $pid = 'pid', $child = '_child', $root=0) {
	$tree = array();// 创建Tree
	if(is_array($list)) {
		// 创建基于主键的数组引用
		$refer = array();
		foreach ($list as $key => $data) {
			$refer[$data[$pk]] =& $list[$key];
		}
		 
		foreach ($list as $key => $data) {
			// 判断是否存在parent
			$parentId = $data[$pid];
			if ($root == $parentId) {
				$tree[$data[$pk]] =& $list[$key];
			}else{
				if (isset($refer[$parentId])) {
					$parent =& $refer[$parentId];
					$parent[$child][] =& $list[$key];
				}
			}
		}
	}
	return $tree;
}

/**
 * 将返回的数据集转换成树
 * @param  array   $list  数据集
 * @return array          转换后的树
 */
function menuList_to_tree($list) {
	$tree = array();// 创建Tree
	if(is_array($list)) {
		// 创建基于主键的数组引用
		$group = array();
// 		$gid = $list[0]["gid"];
// 		$group["id"] = $gid;
// 		$group["text"] = $list[0]["gtitle"];
// 		$group['state']='open';
// 		$group['children']  = getMenuChildList($list,$gid);
// 		$tree[$gid] =$group;
// 		$i =0;
		foreach ($list as $key => $data) {
			if(!array_key_exists($data["gid"],$tree)){
				$gid = $data["gid"];
				$group = array();
				$group["id"] = $gid;
				$group["text"] = $data["gtitle"];
				$group['state']='open';
				$group['children']  = getMenuChildList($list,$gid);
// 				$i ++;
				$tree[$gid] =$group;
			}
		}
	}
	
	return array_values($tree);
}

function getMenuChildList($list,$gid) {

	$child = array();
	foreach ($list as $key => $module) {
		if($module["gid"] ==$gid){
			$module['state']='open';
			$module['attributes']= $module['name'];
			$child[$key] = $module;
		}
	}
	return array_values($child);
}


/**
 * 读取一个表的列信息
 * 
 * @param string $tableName 表名
 * @param array $option 需要获取的属性
 * @return array 返回的一个数组，若指定属性，则以 array("列名1"=>array("属性名1"=>"属性1值"……)……)的格式返回，否则以 array("列名1","列名2"……)的格式返回
 * @example
 */
function  getcolumns($tableName,$option=array()){
	$m=M();
	$columns=array();
	$m_re=$m->query("show columns from `{$tableName}`");
	if(!$m_re){
		return array();
	}
	foreach($m_re as $v){
		$v=array_change_key_case($v);
		if(empty($option)){
			$columns[]=$v["field"];
		}else{
			$vv=array();
			foreach($option as $op){
				$op=strtolower($op);
				if(array_key_exists($op,$v)){
					$vv[$op]=$v[$op];
				}
			}
			$columns[$v["field"]]=$vv;
		}
	}
	return $columns;
}

//用法
//dump(getcolumns("ggw",array("key","type")));
?>